package richbar.com.richbar.fragment.order;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.common.base.BaseFragment;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.order.OrderListAc;
import richbar.com.richbar.adapter.order.OrderAdapter;
import richbar.com.richbar.bean.OrderResult;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;

/**
 * Created by QiYang on 2018/11/29.
 */

public class BaseOrderFragment extends BaseFragment {
    private final static String ACTION = "com.refresh.BaseOrderFragment";
    private MyReceiver myReceiver;
    private SwipeRefreshLayout swipe;
    private RecyclerView recyclerView;
    private OrderListAc activity;
    private OrderAdapter adapter;
    private LoadingDialog loadingDialog;
    String isShipping = "";
    String isPay = "";
    private List<OrderResult.DataBean.ListBean> list = new ArrayList<>();
    private int page = 1;

    public static BaseOrderFragment newInstance(int pos) {
        BaseOrderFragment fragment = new BaseOrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("pos", pos);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base_order, container, false);
        swipe = view.findViewById(R.id.swipe);
        recyclerView = view.findViewById(R.id.recycleView);
        Log.e("AAADDDDDAAA","进来了");
        registerReceiver();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (OrderListAc) context;
    }

    @Override
    protected void initData() {
        loadingDialog = new LoadingDialog(getActivity(), 0);

        int pos = getArguments().getInt("pos");
        /*isShipping	是否发货，1=未发货2=待收货3=已完成
          isPay	        是否付款，1=未付款2=已付款*/
        switch (pos) {
            case 0://全部
                isShipping = "0";
                isPay = "0";
                break;
            case 1://待付款
                isShipping = "0";
                isPay = "1";
                break;
            case 2://待发货
                isShipping = "1";
                isPay = "2";
                break;
            case 3://待收货
                isShipping = "2";
                isPay = "2";
                break;
            case 4://售后
                isShipping = "3";
                isPay = "2";
                break;
        }
        getData(false);


        adapter = new OrderAdapter(getActivity(), loadingDialog, R.layout.item_order, list);
        RecycleViewHelp.setManager(recyclerView, getActivity());
        recyclerView.setAdapter(adapter);
        swipe.setRefreshing(true);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list.clear();
                page = 1;
                getData(false);
            }
        });
        adapter.openLoadMore(true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData(true);
            }
        });
        adapter.setNewData(new ArrayList<OrderResult.DataBean.ListBean>());

    }

    private void getData(final boolean loadMore) {
        LogUtil.e("AAADDDDDAAA","isShipping:" + isShipping + "\n" + "isPay:" + isPay) ;
        NetWorkUtil.getOrderList(page + "", "5", isShipping, isPay, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                refreshDismiss(swipe);
                final OrderResult result = MyGson.getInstance().fromJson(data, OrderResult.class);
                if (checkIsOk(result)) {
                    //adapter.setNewData(result.getData().getList());
                    setData(result, loadMore);
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                refreshDismiss(swipe);
            }
        });
    }

    private void setData(OrderResult result, boolean loadMore) {
        LogUtil.e("DDDDDDDDDDD", 0 + "----" + page);

        if (page > result.getData().getTotalpage()) {
            adapter.isNextLoad(false);
            LogUtil.e("DDDDDDDDDDD", 1);
            return;
        }
        if (loadMore) {
            LogUtil.e("DDDDDDDDDDD", 4);
            //allData.addAll(result.getData().getList());
            list.addAll(result.getData().getList());
            adapter.isNextLoad(true);
        } else {
            LogUtil.e("DDDDDDDDDDD", 5);
            list = result.getData().getList();
            // allData = result.getData().getList();
        }
        adapter.setNewData(list);
    }

    private void registerReceiver(){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION);
        myReceiver = new MyReceiver();
        getActivity().registerReceiver(myReceiver,intentFilter);
    }

    public static void sendBraodcast(Context context){
        Intent intent = new Intent(ACTION);
        context.sendBroadcast(intent);
    }


    class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (null != adapter){
                LogUtil.e("BroadcastReceiver","我刷新了");
                list.clear();
                page = 1;
                getData(false);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(myReceiver);
        myReceiver = null;
    }
}